//
//  TBWithdrawNextVc.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/2/28.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class TBWithdrawNextVc: TTBaseVC {
    
    lazy var moneyField:UITextField = {
        let text = UITextField.init(frame: FRAME_CGFloat(63.5, 75.5, SCREEN_WIDTH - 70, 15))
        text.font = FONT(11)
        return text
    }()
    
    lazy var backView:TBWithdrawBackView = {
        let view = TBWithdrawBackView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP))
        view.isHidden = true
        return view
    }()
    
    lazy var withdrawBtn:UIButton = {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(37.5, 135, SCREEN_WIDTH - 75, 40)
        button.setTitle("提现", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 19)
        button.layer.cornerRadius = 4
        button.addTarget(self, action: #selector(buyBtnAction), for: .touchUpInside)
        button.backgroundColor = RGB_Same(174)
        button.isUserInteractionEnabled = false
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "听币提现"
        
        setTopView()
        setCentreView()
        self.view.addSubview(self.backView)
        backView.delegate = self
        self.moneyField.addTarget(self, action: #selector(textFieldDidChange), for: UIControl.Event.editingChanged)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setMoneyData()
        
    }


}

//  MARK: - ###### UI ######
extension TBWithdrawNextVc {
    func setTopView() {
        SET_LINEVIEW(view: self.view)
        let backView = UIView.init(frame: FRAME_CGFloat(0, 1, SCREEN_WIDTH, 233))
        backView.backgroundColor = .white
        self.view.addSubview(backView)
        
        backView.addSubview(self.moneyField)
        
        let label = UILabel.init(frame: FRAME_CGFloat(37.5, 20.5, 60, 15))
        label.font = FONT(11)
        label.textColor = RGB_Same(53)
        label.text = "提现金额"
        backView.addSubview(label)
        
        let label￥ = UILabel.init(frame: FRAME_CGFloat(37.5, 55.5, 20.5, 47.5))
        label￥.font = Font_Bold(68)
        label￥.textColor = RGB_Same(0)
        label￥.text = "￥"
        backView.addSubview(label￥)
        
        let lineView￥ = UIView.init(frame: FRAME_CGFloat(37.5, 103, SCREEN_WIDTH - 75, 0.5))
        lineView￥.backgroundColor = RGB_Same(240, 1)
        self.view.addSubview(lineView￥)
        
        
        backView.addSubview(withdrawBtn)
    }
    
    func setCentreView() {
        let backView = UIView.init(frame: FRAME_CGFloat(0, 233, SCREEN_WIDTH, 120))
        backView.backgroundColor = .white
        self.view.addSubview(backView)
        
        let lineView = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 1))
        lineView.backgroundColor = RGB_Same(240, 1)
        backView.addSubview(lineView)
        
        let label = UILabel.init(frame: FRAME_CGFloat(17.5, 20, SCREEN_WIDTH - 35, 92))
        label.font = FONT(14)
        label.textColor = RGB_Same(53)
        label.text = "提现规则：\n1.每个用户每日仅能提现一次；\n2.每日最多提现7元；\n3.奖励的金额会存入您的微信零钱包；"
        label.numberOfLines = 4
        backView.addSubview(label)
        
    }
}

//  MARK: - ###### 事件 ######
extension TBWithdrawNextVc {
    func setMoneyData() {
        MineRequestModel.loadWalletBalance(jointArr: [String.getUserId()]) { (success, message, left, right) in
            if success {
                self.moneyField.placeholder = "可提现金额\(String.money(right) ?? "0.00")元"
               
            }
        }
    }
    @objc func buyBtnAction() {
        
        self.backView.isHidden = false
    }
    
    
}

//  MARK: - ###### 协议 ######
extension TBWithdrawNextVc:UITextFieldDelegate,TBWithdrawBackViewDelegate {
    func TBWithdrawBackViewAction() {
        MTProgressHUD.show()
        MineRequestModel.requestWithdrawal(jointArr: [String.getUserId(),0,self.moneyField.text ?? "0"]) { (success, code) in
            MTProgressHUD.hidded(true)
            if success {
                TTAlertView.showMessage(message: code)
                self.withdrawBtn.setTitle("提现审核中", for: .normal)
            } else {
                if code == "022" {
                    TTAlertView.ask(title: nil, question: "不是超级会员不能申请提现，请先去充值成为超级用户", leftTitle: "取消", rightTitle: "充值", okBlock: {
                        let num = self.navigationController?.viewControllers.index(before: 2)
                        self.navigationController?.popToViewController((self.navigationController?.viewControllers[num!])!, animated: true)
                    }, noBlock: {
                        
                    })
                } else if code == "089" {
                    TTAlertView.showMessage(message: "可提现金额必须小于等于余额", second: 2)
                } else if code == "081" {
                    TTAlertView.showMessage(message: "佣金账户不够", second: 2)
                }
                
            }
        }
    }
    
    @objc fileprivate func textFieldDidChange() {
        if checkInput() {
            setLoginBtnStatue(isSelected: true)
        } else {
            setLoginBtnStatue(isSelected: false)
        }
    }
    
    fileprivate func setLoginBtnStatue(isSelected: Bool) {
        if isSelected {
            self.withdrawBtn.backgroundColor = RGB(254, 144, 32)
            self.withdrawBtn.isUserInteractionEnabled = true
        } else {
            self.withdrawBtn.backgroundColor = RGB_Same(174)
            self.withdrawBtn.isUserInteractionEnabled = false
        }
    }
    
    fileprivate func checkInput() -> Bool {
        if !validPrice(money: self.moneyField.text!) {
            return false
        }
        
        return true
    }
}


